<?php

class Model_Translate  extends Ac_Model
{
    const EN      = 'en_US';
    const SC      = 'zh_CN';
    const TC      = 'zh_HK';
    const JP      = 'ja_JP';
    
    protected static $_collection = 'translate';
    //conngroup for user and company models
    protected static $_connectionGroup = 'accounts';
    //db for user and company
    protected static $_db = SHARED_ACCOUNTS_ENV;
    

    public function __construct($data = array(), $config = array())
    {
        $config['db'] = SHARED_ACCOUNTS_ENV;
        $config['collection'] = 'translate';
        parent::__construct($data, $config);
    }

	public function __isset($name) {
        $val = self::__get($name);
        return!empty($val) ? true : parent::__isset($name);
    }

    /**
     *
     * @param string $key Key that will be use to find this translation
     * @param string $value Value for this locale
     * @param string $locale Identify which lanaguage the translation is for
     */
    static public function addTranslation($key, $value, $locale)
    {
        //try to find the key first
        if (null === ($tr = self::one(array('k' => $key)))) {
            $tr         = new Model_Translate();
        }
        $save = false;
        if ($tr->k != $key) {
            $tr->k       = $key;
            $save = true;
        }
        if ($tr->$locale != $value) {
            $tr->$locale = $value;
            $save = true;
        }
        if ($save) {            
            $tr->save();
        }
    }

    /**
     *
     * @return array 
     */
    static public function findLocales()
    {
        return array (self::EN => 'en', self::TC => 'tc', self::SC => 'sc');
    }


    public static function getLocale($key) 
    {
        $arr = self::findLocales();
        $arr = array_flip($arr);
        if (!empty($arr[$key])) {
            return $arr[$key];
        } else {
            return $key;
        }
    }

}

